package com.makefuture.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.ext.interceptor.Restful;
import com.jfinal.kit.HttpKit;
import com.jfinal.render.JsonRender;
import com.makefuture.model.TUser;
import com.makefuture.utils.json.JsonTools;
import com.makefuture.utils.json.JsonTools.TypeEnum;
import com.makefuture.utils.json.JsonValidator;
/**
 * json解析工具类
 * @author ThinkPadCT
 *
 */
public class DataInterface extends Controller{
 
	private static final Class<Map<String, Object>> Map = null;

	@Before(Restful.class) //@Before(Restful.class)  @Before(POST.class)  @Before(GET.class)
	public void index() {
		int t = getParaToInt(0);//http://localhost:8080/makefuture/data/1-2-3
	//	String t =  getPara("t");
	//	String t = getPara("t");//http://localhost:8080/makefuture/data?t=1
		List<TUser> list = TUser.dao.find("select * from t_user where id = "+t+" ");	
		//renderJson(list);
		//JsonKit.toJson(list);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("du","123");
		map.put("user",list);
		//renderJson(JsonKit.toJson(map));//{"du":"123","user":[{"addtime":"2015-01-08 20:13:54","id":1,"name":"管理员","pwd":"123456","roleid":1,"status":"0","tel":"admin"}]}
		//renderJson(JsonKit.toJson(list));//[{"addtime":"2015-01-08 20:13:54","id":1,"name":"管理员","pwd":"123456","roleid":1,"status":"0","tel":"admin"}]
		render( new JsonRender(list).forIE()) ;//解决IE下 成为下载文件的问题
	}
	
	public void recive() {
		 String jsonStr = HttpKit.readData(getRequest());
		 System.out.println(jsonStr);
		// renderJson(map);
		 renderNull();
	}
	
	public static void main(String[] args) throws Exception {
		String json1 = "{\"addtime\":\"2015-01-08 20:13:54\",\"id\":1,\"name\":\"管理员\",\"pwd\":\"123456\",\"roleid\":1,\"status\":\"0\",\"tel\":\"admin\"}";
		String json = "{\"addtime\":\"2015-01-08 20:13:54\",\"id\":1,\"name\":\"管理员\",\"pwd\":\"123456\",\"roleid\":1,\"status\":\"0\",\"tel\":\"admin\",\"addr\":{\"city\":guangzhou,\"province\":guangdong}}";
		String  jsonData = "{\"addr\":[{\"city\":\"guangzhou\",\"province\":\"guangdong\"},{\"city\":\"guiyang\",\"province\":\"guizhou\"}]}";
		String json2 ="{\"page\":{\"pageSize\":\"100\",\"pageIndex\":\"1\"}}"; 
		//HttpKit.post("http://127.0.0.1:8080/makefuture/data/recive",json);
		JsonValidator jv = new JsonValidator();
		Object obj = JsonTools.getObjectByJson(json);//获取整个json数据 
		System.out.println("str:"+obj.toString());
		Object obj1 = JsonTools.getObjectByJson(json1,"addtime",TypeEnum.string);//获取json数据的某个值 单层
		System.out.println("str1:"+obj1.toString());
		Object obj2 = JsonTools.getObjectByJson(jsonData,"addr",TypeEnum.arrayList); //单层 获取list
		System.out.println("str2:"+obj2.toString()+((List)obj2).get(0));
		Object obj3 = JsonTools.getObjectByJson(json2,"page",TypeEnum.map);  //层级递归Map
		System.out.println("str3:"+obj3.toString()+((Map)obj3).get("pageSize"));
		/*********************objec转json**********************************************/
		Map mapPars = new HashMap();
		mapPars.put("agentCode", "SH0001");
		mapPars.put("date", "2014-01-10");
		mapPars.put("url", "http://www.map.com/maps.jsp?tag=2");
		Map mapArgs = new HashMap();
		mapArgs.put("query", mapPars);
		Map maps = new HashMap();
		maps.put("request", mapArgs);
		maps.put("date", "2014-10-10");
		System.out.println(JsonTools.getObjectByJson(maps));
//		
//		String jsons = "{\"request\":{\"query\":[{\"date\":\"2014-01-10\",\"agentCode\":\"SH0001\",\"url\":\"http://www.map.com/maps.jsp?tag=2\"}]},\"date\":\"2014-10-10\"}";
	
////		
	     String jsonStr =  "{\"api\":\"2.1\",\"message\":[\"产品\",\"tokken\"],\"request\":{\"ptype\":\"JK\",\"tokken\":\"A#daDSFkiwi239sdls#dsd\"},\"response\":{\"status\":{\"statusCode\":\"500\",\"statusMessage\":[\"产品类型错误\",\"tokken失效\"]},\"page\":{\"pageSize\":\"100\",\"pageIndex\":\"1\"},\"data\":{\"ptitle\":\"all product lists\",\"sDate\":\"2014-12-01\",\"eDate\":\"2016-12-01\",\"productList\":[{\"pid\":\"RA001\",\"pname\":\"产品1\"},{\"pid\":\"RA002\",\"pname\":\"产品2\"}]}},\"args\":[{\"tit\":\"RA001\",\"val\":\"产品1\"},{\"tit\":\"RA002\",\"val\":\"产品2\"}]}";
			Object obj5 = JsonTools.getObjectByJson(jsonStr,"response.data.productList",TypeEnum.arrayMap);   //层级递归ArrayMap 
			System.out.println(obj5);
		
		String jsons = "{\"addr\":[{\"city\":\"guangzhou\",\"province\":\"guangdong\"},{\"city\":\"guiyang\",\"province\":\"guizhou\"}]}";  
	    ObjectMapper mapper = new ObjectMapper();
	    Object maps1 = JsonTools.getObjectByJson(jsons,"addr", TypeEnum.arrayMap);
		System.out.println(maps1.toString());
	}
}
